﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace Microsoft.Office.OneNote.XmlViewer
{
    internal sealed partial class AboutForm : Form
    {
        public AboutForm()
        {
            InitializeComponent();

            string description = AssemblyTitle;

            Text = String.Format("About {0}", description);
            
            lblDescription.Text = description;
            lblCopyright.Text = AssemblyCopyright;
            lblVersion.Text = AssemblyVersion;
        }

        private string AssemblyVersion
        {
            get
            {
                return Get<AssemblyFileVersionAttribute>(a => a.Version);
            }
        }

        private string AssemblyCopyright
        {
            get
            {
                return Get<AssemblyCopyrightAttribute>(a => a.Copyright);
            }
        }

        private string AssemblyTitle
        {
            get
            {
                return Get<AssemblyTitleAttribute>(a => a.Title);
            }
        }

        private string Get<TAttribute>(Func<TAttribute, String> extractor)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            TAttribute attr = (TAttribute)asm.GetCustomAttributes(typeof(TAttribute), false).SingleOrDefault();

            if (attr != null)
            {
                return extractor(attr);
            }

            return string.Empty;
        }
    }
}
